﻿using BepInEx;
using System;
using UnityEngine;
using HarmonyLib;
using UnityEngine.UI;
using System.Reflection;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using TMPro;

namespace tiandiguixu.foring
{

    /// <summary>
    /// 如果有坑的话
    /// 每回合保持一个神庙，道观
    /// </summary>
    public partial class ForingMod
    {
        //private int index = 0;
        //public bool ChangeChengHaoEnable = false;
        //public static TextMeshProUGUI CharaNameUI = null;
        //private static CharaInteractType charaInteractType;
        //private static Text CharaInteractPanel_replyText;
        //private static Transform CharaInteractPanel_optionParent;
        //private static Chara CharaInteractPanel_interactChara;
        //private static TextMeshProUGUI GetCharaNameUI()
        //{
        //    if (CharaNameUI != null) return CharaNameUI;
        //    var field = CharaInfoPanel_New.Instance.GetType().GetField("charaName", BindingFlags.NonPublic | BindingFlags.Instance);
        //    TextMeshProUGUI charaName = field.GetValue(CharaInfoPanel_New.Instance) as TextMeshProUGUI;
        //    CharaNameUI = charaName;
        //    return CharaNameUI;
        //}

        //private static Text GetCharaInteractPanel_replyText()
        //{
        //    if (CharaInteractPanel_replyText != null) return CharaInteractPanel_replyText;
        //    CharaInteractPanel_replyText = CharaInteractPanel.Instance.transform.Find("ReplyPanel").Find("Text").GetComponent<Text>();
        //    return CharaInteractPanel_replyText;
        //}

        //private static Transform GetCharaInteractPanel_optionParent()
        //{
        //    if (CharaInteractPanel_optionParent != null) return CharaInteractPanel_optionParent;
        //    CharaInteractPanel_optionParent = CharaInteractPanel.Instance.transform.Find("Options");
        //    return CharaInteractPanel_optionParent;
        //}

        ///// <summary>
        ///// 打开对话框前记录对话类型
        ///// </summary>
        ///// <param name="charaId"></param>
        ///// <param name="interactType"></param>
        //[HarmonyPrefix]
        //[HarmonyPatch(typeof(CharaInteractPanel))]
        //[HarmonyPatch("Refresh", new Type[] { typeof(string), typeof(CharaInteractType) })]
        //public static void CharaInteractPanel_Refresh(string charaId, CharaInteractType interactType)
        //{
        //    if (!instance.enabelChengHao.Value || !WholeObjects.Instance.Dic_Chara.ContainsKey(charaId)) return;
        //    charaInteractType = interactType;
        //    CharaInteractPanel_interactChara= WholeObjects.Instance.Dic_Chara[charaId];
        //    instance.Logger.LogInfo("对话类型" + "  " + interactType + "角色id" + charaId + "  "+ CharaInteractPanel_interactChara?.name);

        //}

        ///// <summary>
        ///// 打开对话框前添加标签
        ///// </summary>
        ///// <param name="options"></param>
        //[HarmonyPrefix]
        //[HarmonyPatch(typeof(CharaInteractPanel))]
        //[HarmonyPatch("RefreshOptions")]
        //public static void CharaInteractPanel_RefreshOptions(ref Option[] options)
        //{
        //    if (!instance.enabelChengHao.Value) return;
        //    instance.Logger.LogInfo("打开对话框前添加标签 " + charaInteractType); 
        //    if (charaInteractType != CharaInteractType.门派人物) return;
        //    if (options == null || options.Length == 0) return;
        //    if (options[0].command == "闲聊")
        //    {
        //        var list = new List<Option>(options);
        //        list.Insert(options.Length-1 ,new Option("闲聊#称号", "门派称号"));
        //        options = list.ToArray();
        //    }
        //}

        ///// <summary>
        ///// 打开对话框前添加标签
        ///// </summary>
        ///// <param name="options"></param>
        //[HarmonyPrefix]
        //[HarmonyPatch(typeof(CharaInteractPanel))]
        //[HarmonyPatch("PareseOptionClickCommand")]
        //public static bool CharaInteractPanel_PareseOptionClickCommand(ref Option option)
        //{
        //    if (!instance.enabelChengHao.Value) return true;
        //    instance.Logger.LogInfo("点击对话" + "  " + option.command + "  " + option.text);

        //    if (option.command == "称号#返回")
        //    {
        //        option = new Option("刷新#主界面", "换个话题。（返回）");
        //        return true;
        //    }

        //    if (option.command == "闲聊#称号" && option.text == "门派称号")
        //    {
        //        var RefreshDefaultCharaInteractMethond = CharaInteractPanel.Instance.GetType().GetMethod("RefreshDefaultCharaInteract", BindingFlags.Instance | BindingFlags.NonPublic);

        //        instance.Logger.LogInfo(RefreshDefaultCharaInteractMethond);
        //        if (RefreshDefaultCharaInteractMethond != null)
        //        {
        //            var list = new List<Option>(instance.ChengHaos.Value.Split('|').Select(i => new Option("称号", i)).ToArray());
        //            //list.Add(new Option("刷新#主界面", "换个话题。（返回）"));
        //            list.Add(new Option("称号#返回", "返回"));
        //            RefreshDefaultCharaInteractMethond.Invoke(CharaInteractPanel.Instance, new object[]
        //            {
        //                "师傅要给什么称号弟子。" ,
        //                list.ToArray()
        //            });
        //        }
        //        return false;
        //    }
        //    if (option.command == "称号")
        //    {
        //        var RefreshDefaultCharaInteractMethond = CharaInteractPanel.Instance.GetType().GetMethod("RefreshDefaultCharaInteract", BindingFlags.Instance | BindingFlags.NonPublic);

        //        instance.Logger.LogInfo(RefreshDefaultCharaInteractMethond);
        //        if (RefreshDefaultCharaInteractMethond != null)
        //        {
        //            var list = new List<Option>(option.text.Split('、').Select(i => new Option("称号定为", i)).ToArray());
        //            //list.Add(new Option("刷新#主界面", "换个话题。（返回）"));
        //            list.Add(new Option("称号定为", "删除称号"));
        //            list.Add(new Option("称号#返回", "返回"));
        //            RefreshDefaultCharaInteractMethond.Invoke(CharaInteractPanel.Instance, new object[]
        //            {
        //                "师傅要给弟子什么称号。" ,
        //                list.ToArray()
        //            });
        //        }
        //        return false;
        //    }

        //    if (option.command == "称号定为")
        //    {
        //        if (option.text == "删除称号")
        //        {
        //            instance.Logger.LogInfo(CharaInteractPanel_interactChara.name + " 删除称号 ");
        //            CharaInteractPanel_interactChara.name = CharaInteractPanel_interactChara.name.Split('·').LastOrDefault();
        //            SectCharaListPanel.instance.Fresh();
        //            option = new Option("关闭", "退下吧。（离开）");
        //            return true;
        //        }
        //        instance.Logger.LogInfo(CharaInteractPanel_interactChara.name+" 称号定为 " + "  " + option.text);
        //        CharaInteractPanel_interactChara.name = option.text + "·" + CharaInteractPanel_interactChara.name.Split('·').LastOrDefault();
        //        //CharaInteractPanel_interactChara = null;
        //        //CharaInteractPanel.Instance.gameObject.SetActive(false);
        //        //option=new Option("关闭", "退下吧。（离开）");
        //        //var RefreshCharaInteractMethond = CharaInteractPanel.Instance.GetType().GetMethod("RefreshCharaInteract", BindingFlags.Instance | BindingFlags.NonPublic);
        //        //var mainInteractField= CharaInteractPanel.Instance.GetType().GetField("mainInteract", BindingFlags.Instance | BindingFlags.NonPublic);
        //        //var mainInteract = mainInteractField.GetValue(CharaInteractPanel.Instance) as CharaInteract;
        //        //RefreshCharaInteractMethond.Invoke(CharaInteractPanel.Instance, new object[] { mainInteract,null});
        //        SectCharaListPanel.instance.Fresh();
        //        option = new Option("关闭", "退下吧。（离开）");
        //        return true;
        //    }
        //    return true;
        //}
    }
}
